package com.atguigu.juc;


import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author ：MJS
 * @date ：Created in 2021/4/23  20:44
 * @description：线程     操纵       资源
 */
public class SaleTicket {
    public static void main(String[] args) {
        Tickets tickets = new Tickets();
        new Thread(()->{ for (int i = 0; i <=31; i++) { tickets.sale(); } },"张三").start();
        new Thread(()->{ for (int i = 0; i <=31; i++) { tickets.sale(); } },"李四").start();
        new Thread(()->{ for (int i = 0; i <=31; i++) { tickets.sale(); } },"王五").start();

    }
}

class Tickets{
    private int number=30;
    Lock lock=new ReentrantLock();

    public void sale(){
        lock.lock();
        try {
            if(number>0){
            System.out.println(Thread.currentThread().getName()+"\t"+"卖了第："+(number--)+"\t还剩下"+number);
            }
            }finally {
            lock.unlock();
        }

    }
}


